package com.xinli.business.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xinli.business.domain.Invitee;
import com.xinli.business.domain.Inviter;
import com.xinli.business.domain.MeetingRequest;
import com.xinli.common.utils.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;



@Service
public class MeetingReservationClient {
 public  static   String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA19+aVXw0qCKRLbWv4btVDOl3PYlfx3nW81khnOJRjZsB0C+sP74o6MXsy30GC+kr/NBwlvRG122Mmvuxb6ygiW0GlslkKM1sE1v/QFlTgn6HFL6iHgoBoMzVLm3VsXY/bN3RpaiGK51l/kEVTHCxP08vzlgqHUwKJVgKsEWAJZTcu4HcNrCIU7jhNADBX8oM3eViTiIdjgozkzVbU9/sfUonI0oI4jUrXu0cZddlJFw4fWWOc1mcBjt1jXXm2KXisvoWTzQv5B1lAn7LO1+DXUdNnVK0XlK+nCdigoIWyy+fl7hia3OmOs53NUGebUetpEzzhTwezQh0anyQBTT+ZwIDAQAB";


    private final RestTemplate restTemplate;

    public MeetingReservationClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }


    public   String reserveMeeting(String  meetingtitle,String startIime,String endTime,Inviter inviter ,Invitee invitee) {
        String meetingInfo = "";
        String url = "https://discover.t.nxin.com/api/open/meeting/reservation";
        // 构建被邀请人列表
//        Invitee invitee = new Invitee("王华", "", "13101466306");
//        // 构建邀请人
//        Inviter inviter = new Inviter("张丹老师", "", "18601466306");
        // 构建请求体
        MeetingRequest request = new MeetingRequest(
                164,
                meetingtitle,
                "",
                startIime,
                endTime,
                meetingtitle,
                inviter,
                Collections.singletonList(invitee)
        );
        // 将对象序列化为 JSON 字符串（需引入 Jackson 或 Gson）
        String jsonBody = JSONUtil.toJsonStr(request); // 假设你有 JSON 序列化工具类
        // 使用公钥加密 JSON 字符串
        String encryptedData = RsaUtil.encryptByPublicKey(jsonBody, publicKey);
        JSONObject  finalRequestBody =new JSONObject();
        finalRequestBody.set("encryption_data", encryptedData);
        // 转换为字符串用于发送
        String requestBodyStr = finalRequestBody.toString();
        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 发送加密后的请求体
        HttpEntity<String> entity = new HttpEntity<>(requestBodyStr, headers);

        // 发送 POST 请求
        try {
            String response = restTemplate.postForEntity(url, entity, String.class).getBody();
           //{"code":"0","data":{"meetingNo":"251299581","meetingPassword":"123458",
            // "roomNo":"174677615400000001","meetingStartTime":"2025-05-08 19:00:00","appUrl":"https://discovermc.nxin.com/#/download",
            // "meetingTitle":"心理咨询科","meetingUrl":"https://live.t.nxin.com/home?roomId=251299581",
            // "meetingRemark":"请提前预约心理课程1","inviterName":"张丹老师","meetingEndTime":"2025-05-08 21:00:00"},
            // "exception":"success","msg":"成功","timestamp":"1746776155995"}
            // 解析这个返回值
            JSONObject responseJson = JSONUtil.parseObj(response);
            if (responseJson.getInt("code") == 0) {
                JSONObject data = responseJson.getJSONObject("data");
                meetingInfo=data.toString();
                System.out.println("会议编号: " + data.getStr("meetingNo"));
                System.out.println("会议密码: " + data.getStr("meetingPassword"));
                System.out.println("会议开始时间: " + data.getStr("meetingStartTime"));
                System.out.println("会议结束时间: " + data.getStr("meetingEndTime"));
                System.out.println("会议标题: " + data.getStr("meetingTitle"));
                System.out.println("会议描述: " + data.getStr("meetingRemark"));
                System.out.println("会议邀请人: " + data.getStr("inviterName"));
                System.out.println("会议链接: " + data.getStr("meetingUrl"));
                System.out.println("会议appUrl: " + data.getStr("appUrl"));
                System.out.println("会议房间号: " + data.getStr("roomNo"));
                System.out.println("会议密码: " + data.getStr("meetingPassword"));

            }
            System.out.println("Response: " + response);
        } catch (Exception e) {
            System.err.println("请求失败: " + e.getMessage());
            e.printStackTrace();
        }
        return meetingInfo;
    }


}
